<?php

/**
 * This file is part of the Codizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Codizer Core Team <team@codizer.org>
 * @package     Codizer
 */

class f_utils_Js
{	
	/**
	 * Format a php array to a javascript array
	 *
	 * @param string $arrayName
	 * @param array $phpArray
	 * 
	 * @return string js array
	 */
	public static function phpArrayToJsArray($arrayName, $phpArray)
	{
		$result = printf("%s = new Array();\n", $arrayName);
		
		foreach($phpArray as $keyItems => $valueItems)
		{			
			if(is_array($valueItems))
			{
				$result .= printf("%s[%s]= new Array();\n",$arrayName, $keyItems);
				
				foreach($valueItems as $keyItem => $valueItem)
				{	
					$result .= printf("%s[%s][%s] = %s;\n",$arrayName, $keyItems, $keyItem,	is_string($valueItem) ?  "\"".addslashes($valueItem)."\"" : $valueItem);
				}
			}
			else
			{
				$result .= printf("%s[%s] = %s;\n", $arrayName, $keyItems, is_string($valueItems) ?  "\"".addslashes($valueItems)."\"" : $valueItems);
			}
		}
		
		return $result;
	}
}